package com.mbc.common.manager;

import java.util.List;

import com.mbc.common.entities.Notification;

public class NotificationMgr extends AbstractMgr {
	
	public NotificationMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public List<Notification> getNotifications(long userId)throws Exception{
		openConnection();
		try {
			return entityManager.createNamedQuery("Notification.findByUserId", Notification.class)
					.setParameter("notified", false)
					.setParameter("userId", userId)
					.setHint("org.hibernate.cacheable", false)
					.getResultList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void updateNotifiedStatus(long id)throws Exception{
		openConnection();
		try {
			beginTransaction();
			Notification l_noti = entityManager.find(Notification.class, id);
			l_noti.setNotified(true);
			entityManager.merge(l_noti);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
}
